package com.example.demo.service;

import com.ces.utils.TokenUtils;
import com.example.demo.entity.Areas;
import com.example.demo.entity.Employee;
import com.example.demo.entity.Users;
import com.example.demo.entity.UsersDto;
import com.example.demo.mapper.EmployeeDao;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.UserMapperDao;
import com.example.demo.utils.AppDataUtil;
import com.example.demo.utils.HttpClientUtil;
import com.example.demo.utils.PageDataList;
import com.example.demo.utils.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;
import java.util.*;


@Service
public class UserService {

  @Autowired
  private UserMapper userMapper;
  @Autowired
  private UserMapperDao userMapperDao;

  @Autowired
  private AppDataUtil appDataUtil;


  private HttpClientUtil httpClientUtil = null;

  public ResultUtil userLogin(HttpSession session, Users input) {
    ResultUtil resultUtil = new ResultUtil();
    try {
      Users users = userMapper.userLogin(input);
      session.setAttribute("userName", users.getUserName());
      String userName = session.getAttribute("userName").toString();
      System.out.println("userName:" + userName);
      if (users != null) {
        if (!users.getUserStatus().equals("1")) {
          resultUtil.setResultCode("204");
          resultUtil.setResultMsg("账号禁用");
        } else {
          resultUtil.setResultCode("200");
          resultUtil.setResultMsg("请求成功");
          resultUtil.setResultData(users);
        }
      } else {
        resultUtil.setResultCode("201");
        resultUtil.setResultMsg("请求失败");
      }
    } catch (Exception e) {
      e.printStackTrace();
      resultUtil.setResultCode("202");
      resultUtil.setResultMsg("用户名或者密码错误");
    }
    return resultUtil;
  }


  public PageDataList<Users> getList(UsersDto usersDto) {
    List<Users> resultList = new ArrayList<>();
    PageHelper.startPage(usersDto.getPageNumber(), usersDto.getPageSize());
    List<Users> dbResult = userMapper.getList(usersDto);

    Page<Users> dbResultPage = (Page<Users>) dbResult;
    for (Users dbItem : dbResultPage.getResult()) {
      resultList.add(dbItem);
    }

    for(int i  = 0;i<resultList.size();i++){
      userMapperDao.save(resultList.get(i));
    }

    PageDataList<Users> pageDataList = new PageDataList<>(dbResultPage.getTotal(),
            dbResultPage.getPages(),
            dbResultPage.getPageNum(), dbResultPage.getPageSize());
    pageDataList.setList(resultList);
    return pageDataList;

  }



  public Users queryUserById(UsersDto usersDto) {
    Users users = userMapperDao.queryUsersById(usersDto.getId().toString());
    return users;

  }



  public String deleteUserById(UsersDto usersDto) {
    Users users = userMapperDao.queryUsersById(usersDto.getId().toString());
    userMapperDao.delete(users);
    System.err.println("delete a obj");
    return "success";
  }



}
